﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace newTurtle8
{
    public partial class TahasukaWorkingRequestCard : UserControl
    {
        public static readonly string TableName = "TahasukaRequests";
        Tahasuka tahasuka;
        DataTable dt = new DataTable();
        SqlDataAdapter da;

        //Ctor
        public TahasukaWorkingRequestCard(Tahasuka tahasuka)
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;

            this.tahasuka = tahasuka;
            tahasuka.ZimunimList_Modified += new Mofa.ZimunimListChangedHandler(refresh);

            da = SqlManager.SqlDataAdapterFactory(SqlManager.getSqlCommandFromFile(@"\SELECT\TahasukaRequests.SELECT.sql"));
            da.UpdateCommand = SqlManager.getSqlCommandFromFile(@"\UPDATE\TahasukaRequests.UPDATE.sql");

            //sets SELECT parameters
            da.SelectCommand.Parameters.Add("@start", SqlDbType.Date).Value = tahasuka.StartDate;
            da.SelectCommand.Parameters.Add("@finish", SqlDbType.Date).Value = tahasuka.EndDate;   

            //sets UPDATE parameters
            da.UpdateCommand.Parameters.Add("@armyID", SqlDbType.NVarChar);
            da.UpdateCommand.Parameters.Add("@start", SqlDbType.Date).Value = tahasuka.StartDate;
            da.UpdateCommand.Parameters.Add("@finish", SqlDbType.Date).Value = tahasuka.EndDate;
            da.UpdateCommand.Parameters.Add("@comments", SqlDbType.NVarChar);
                 
            refresh();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            BaseCollection rows = dataGridView.SelectedRows;
            int requestID_index = dataGridView.Columns["requestID"].Index;
            List<int> requestIdList = new List<int>();

            foreach (DataGridViewRow row in rows)
                requestIdList.Add((int)row.Cells[requestID_index].Value);

            tahasuka.removeRequests(requestIdList);
            refresh();
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            SoldiersView z_list = new SoldiersView(tahasuka, SoldiersView.Type.Called, new DelegateHandler(tahasuka.addRequest));
            z_list.Text = "רשימת לוחמים שזומנו לתעסוקה";
            z_list.ShowDialog();

            refresh();
        }

        public void refresh()
        {
            dt.Clear();
            da.Fill(dt);
            dataGridView.DataSource = dt;
           
            //set readonly columns
            dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridView.Columns["firstName"].ReadOnly = true;
            dataGridView.Columns["lastName"].ReadOnly = true;
            dataGridView.Columns["armyID"].ReadOnly = true;
            dataGridView.Columns["comments"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView.Columns["requestID"].Visible = false;

            Translator.setTableColumnHeadersHebrew(dataGridView.Columns);
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            labelSuccessfulUpdate.Visible = false;
        }

        private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            da.Update(dt);
        }

       
    }
}
